

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>文本转换声音</title>
<style type="text/css">
*{ margin:0 auto}
.iphone{width:680px;box-shadow: 0 3px 7px 2px rgba(4,0,0,.2); padding:8px; text-align:center;min-height:500px; }
a{ text-decoration:none; color:#000000}
</style>
</head>

<body>
<div class="iphone">
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
<div style="text-align:left; font-size:12px"><a href="http://www.zluck.cn/" target="_blank">返回首页</a></div>
<br>网页版文本转换语音工具<br><br />
<textarea rows="9" cols="39" name="text">
欢迎使用文本转换语音工具
</textarea>
<br /><br />
声音选择
<select name="per">
    <option value="0">女声</option>
    <option value="1">男声</option>
    </select>
<br /><br />
<input type="submit" value="播放">
</form>
<br />
<?php

    $text= urlencode($_POST["text"]);//要转换的文字
	$ctp=  1; //web使用
	$per=  $_POST["per"];//声音选择0 为女声，1 为男声
if(!empty($text)){
    $ch = curl_init();
    $url = "http://apis.baidu.com/apistore/baidutts/tts?text=$text&ctp=$ctp&per=$per";
    $header = array(
        'apikey: b8a7eee29f2f567156d52e27d0ec07b6',
    );
    // 添加apikey到header
    curl_setopt($ch, CURLOPT_HTTPHEADER  , $header);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    // 执行HTTP请求
    curl_setopt($ch , CURLOPT_URL , $url);
    $res = curl_exec($ch);

   // var_dump(json_decode($res));

	$obj = json_decode($res);
	$base= $obj->retData; //想要读取的值
	echo $_POST["text"];
   }else{
	   
	   echo "请输入要转换的文本！";
   }
   
?>

<br/><br />
<audio controls autobuffer="autobuffer" autoplay> 
    <source src="data:audio/mp3;base64,<?php echo $base;?>" /> 
	您的浏览器不支持 audio 标签。
</audio> 
<div>此应用由 <a href="http://www.wyjwz.com" target="_blank">我要建网站</a> 制作</div>
</div>
<div style="display:none"><script type="text/javascript">var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");document.write(unescape("%3Cspan id='cnzz_stat_icon_1255665031'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s4.cnzz.com/z_stat.php%3Fid%3D1255665031%26show%3Dpic1' type='text/javascript'%3E%3C/script%3E"));</script></div>
</body>
</html>